package com.ccys.cms.service.corporateAndInsurance.impl;

import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.corporateAndInsurance.CDMPBenefitPlanService;
import com.ccys.cms.service.corporateAndInsurance.CDMPBenefitService;
import com.ccys.cms.service.corporateAndInsurance.CDMPService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitSService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.CDMPBenefitDto;
import com.ccys.db.dto.corporateAndInsurance.CDMPBenefitPageDto;
import com.ccys.db.entity.TblCdmp;
import com.ccys.db.entity.TblCdmpBenefit;
import com.ccys.db.entity.TblCdmpBenefitPlan;
import com.ccys.db.entity.TblPlanBenefitService;
import com.ccys.db.mapper.TblCdmpBenefitMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.corporateAndInsurance.CDMPBenefitVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CDMPBenefitSeriviceImpl extends BaseServiceImpl<TblCdmpBenefit> implements CDMPBenefitService {

    @Resource
    private TblCdmpBenefitMapper mapper;

    @Resource
    private PlanBenefitSService benefitSService;

    @Resource
    private CDMPBenefitPlanService planService;

    @Resource
    private CDMPService cdmpService;

    @Override
    public ResponseResult getPage(CDMPBenefitPageDto dto) {

        PageDto.startPage();
        List<CDMPBenefitVo> list = mapper.getList(dto);
        PageInfo<CDMPBenefitVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);

    }

    @Override
    public void deleteBenefit(String planId) {
        mapper.deleteBenefit(planId);
    }

    @Override
    public ResponseResult change(CDMPBenefitDto dto) {

        TblCdmpBenefit cdmpBenefit = dto.getCdmpBenefit();
        TblPlanBenefitService benefitService = dto.getBenefitService();
        int i = this.insertOrUpdate(cdmpBenefit);
        benefitService.setBenefitId(cdmpBenefit.getId());
        benefitSService.insertOrUpdate(benefitService);
        List<TblCdmpBenefitPlan> policyList = dto.getPolicyList();
        //添加修改时根据外层 CDMP 的type 添加 benefit plan
        //查询 CDMP 的类型
        if (CollectionUtils.isNotEmpty(policyList)) {
            TblCdmp entityById = cdmpService.findEntityById(cdmpBenefit.getPlanId());
            // 0:retail;1:AIA corporate;2:AIA health shield;3:Direct corporate;4:Subscription
            Integer type = entityById.getType();
            //type是 0 或 2 就不管
            if (StringUtils.isEmpty(cdmpBenefit.getId())) {
                if (type != 0 && type != 2) {
                    policyList.forEach(x -> {
                        x.setBenefitId(cdmpBenefit.getId());
                        x.setId(IdUtil.getSnowflakeNextIdStr());
                    });
                }
            } else {
                //先删除policyList
                planService.deletePlan(cdmpBenefit.getId());
            }
            planService.insertAll(policyList);
        }
        return ResponseResult.update(i);
    }
}
